package View;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.Iterator;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.Border;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import Model.ElementSlide;
import Model.ImageElement;
import Model.ListSlideModel;
import Model.SlideContent;
import Model.TextElement;

import kdiapo.CreationFrame;
import kdiapo.Main;

public class SnapShotView extends JPanel{
	private int numSlide;
	private CreationFrame mainFrame;
	private JLabel lb;
	
	public SnapShotView(CreationFrame f,int num,JPanel currentSlide){
	
		this.mainFrame = f;
		this.numSlide = num;
		
		this.setFocusable(true);
		this.requestFocus();
		
		this.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
		this.setBorder(BorderFactory.createLineBorder(Color.red));
		lb = new JLabel();
		this.add(lb);
		
		ImageIcon im = new ImageIcon(this.takeSnapShot(currentSlide));
    	
		lb.setIcon(im);
		
		this.setFocusable(true);
		
		
		addListeners();
	}
	
	public void addListeners(){
		
		this.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger())
		            doPop(e);
			}
			
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger())
		            doPop(e);
			}
			
			public void mouseExited(MouseEvent e) {}
			
			public void mouseEntered(MouseEvent e) {}
			
			public void mouseClicked(MouseEvent e) {
				requestFocus();
				setBorder(BorderFactory.createLineBorder(Color.red));
				mainFrame.getModel().setCurrentSlideForModel(numSlide);
			}
			
			public void doPop(MouseEvent e){
				JPopupMenu popMenu = new JPopupMenu();
				JMenuItem anItem =  new JMenuItem("Remove this slide");
				anItem.setIcon(new ImageIcon(Main.class.getResource("/remove_icon.png")));
				
				ListSlideModel md = mainFrame.getModel();
				if(md.getNbSlide()>1){
					anItem.setEnabled(true);
				}else{
					anItem.setEnabled(false);
				}
				
				anItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						removeSlide(numSlide);
					}
				});
				
				popMenu.add(anItem);
				popMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});
		
		this.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent arg0) {
				
			}
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				if(e.getKeyCode() == KeyEvent.VK_DELETE){
					removeSlide(numSlide);
				}
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public void removeSlide(int num){
		ListSlideModel md = mainFrame.getModel();
		if(md.getNbSlide()>1){
			int newCurrentSlide = md.removeSlide(num);
			mainFrame.printModel();
			mainFrame.removeSlide(num,newCurrentSlide);
		}
	}
	public int getNumSlide() {
		return numSlide;
	}

	public void setNumSlide(int numSlide) {
		this.numSlide = numSlide;
	}

	public void updateSnapShot(JPanel currentSlide){
		ImageIcon im = new ImageIcon(this.takeSnapShot(currentSlide));
    	
		lb.setIcon(im);
		this.validate();
		this.repaint();
	}
	
	public BufferedImage  takeSnapShot(JPanel panel){  
    	BufferedImage bufImage = new BufferedImage(panel.getSize().width, panel.getSize().height,BufferedImage.TYPE_INT_RGB);  
    	panel.paint(bufImage.createGraphics());  
    	return ImageView.resize(bufImage,140,140);
	}

}
